package com.maimao.model.dynamicform.validate;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.maimao.model.goods.enums.FormType;
import com.maimao.model.goods.vo.SpecDetailVo;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2020/12/19 9:39 下午
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Validate implements Serializable {

    private static final long serialVersionUID = -1567265242667658050L;
    /**
     * 下拉框需要设置为array
     */
    private String type;
    private boolean required;
    private String message;
    /**
     * blur or change
     */
    private String trigger;

    public Validate() {

        required = true;
        message = "请填写%s";
    }

    public static List<Validate> getDefaultValidate(SpecDetailVo spec) {
        List<Validate> validates = new ArrayList<>(1);
        Validate validate = new Validate();
        validate.setTrigger("blur");
        validate.setMessage(String.format(validate.getMessage(), spec.getName()));
        validates.add(validate);
        return validates;
    }

    public static List<Validate> getSelectValidates(SpecDetailVo spec,String formType) {
        List<Validate> validates = new ArrayList<>(1);
        Validate validate = new Validate();
        validate.setTrigger("change");
        validate.setMessage("请选择" + spec.getName());
        if (formType.equals(FormType.MULTI_SELECT.getName())){
            validate.setType("array");
        }
        validates.add(validate);
        return validates;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTrigger() {
        return trigger;
    }

    public void setTrigger(String trigger) {
        this.trigger = trigger;
    }
}
